package com.yang.studentsapp.ui.adapter;

import android.graphics.Bitmap;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yang.studentsapp.AdapterClickBack;


/**
 * Created by LongWH on 2016/9/22.
 * All Rights Reserved by ZhiYou @2016 - 2017
 */
public class ViewHolder extends com.yang.studentsapp.ViewHolder implements View.OnClickListener {
    private final String mTag = getClass().getName();
    private SparseArray<View> mSparseArray = new SparseArray<>();
    private View mConvertView;

    public View getConvertView() {
        return mConvertView;
    }

    public ViewHolder setConvertViewBackGround(int colorRes){
        if(mConvertView != null){
            mConvertView.setBackgroundResource(colorRes);
        }
        return this;
    }

    private int mPosition;
    private AdapterClickBack mAdapterClickBack;

    public ViewHolder(AdapterClickBack onClickBack, View convertView) {
        mAdapterClickBack = onClickBack;
        mConvertView = convertView;
        if (mConvertView != null) {
            mConvertView.setTag(this);
        }
    }

    public int getPosition() {
        return mPosition;
    }

    public void setPosition(int position) {
        mPosition = position;
    }

    private <T extends View> T findViewById(int viewId) {
        if (mConvertView == null) {
            Log.e(mTag, "Error, mConvertView is null !!!");
            return null;
        }
        View view = mConvertView.findViewById(viewId);
        if (view == null) {
            Log.e(mTag, "Error, viewId is not found !!!");
            return null;
        }
        mSparseArray.put(viewId, view);
        return (T) view;
    }

    public <T extends View> T getView(int viewId) {
        View view = mSparseArray.get(viewId);
        if (view != null) {
            return (T) view;
        }
        findViewById(viewId);
        return (T) mSparseArray.get(viewId);
    }

    public ViewHolder setText(int text, int viewId) {
        View view = getView(viewId);
        if (view != null && view instanceof TextView) {
            ((TextView) view).setText(text);
        }
        return this;
    }

    public ViewHolder setText(CharSequence text, int viewId) {
        View view = getView(viewId);
        if (view != null && view instanceof TextView) {
            ((TextView) view).setText(text);
        }
        return this;
    }

    public ViewHolder setImageBitmap(Bitmap bitmap, int viewId) {
        if (bitmap == null) {
            Log.e(mTag, "Error, bitmap is null !!!");
            return this;
        }
        View view = getView(viewId);
        if (view != null && view instanceof ImageView) {
            ((ImageView) view).setImageBitmap(bitmap);
        }

        return this;
    }

    public ViewHolder setImageRes(int imgResId,int viewId) {
        View view = getView(viewId);
        if (view != null && view instanceof ImageView) {
            ((ImageView) view).setImageResource(imgResId);
        }
        return this;
    }

    public ViewHolder setOnclickListener(int viewId) {
        View view = getView(viewId);
        if (view != null) {
            view.setOnClickListener(this);
        }
        return this;
    }

    /**处理控件的显示与隐藏
     * @param visible true 则显示，false 则 Gone
     * @param viewId ：
     * @return ：
     */
    public ViewHolder setVisible(boolean visible,int viewId) {
        View view = getView(viewId);
        if (view != null) {
            view.setVisibility(visible ? View.VISIBLE : View.GONE);
        }

        return this;
    }
    public ViewHolder setVisibleOrInvisible(boolean visible,int viewId) {
        View view = getView(viewId);
        if (view != null) {
            view.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
        }

        return this;
    }




    @Override
    public void onClick(View v) {
        if (mAdapterClickBack != null) {
            mAdapterClickBack.onClickBack(v,mPosition,this);
        }
    }
}
